#include "Octopi.h"

#include <iostream>

Octopi::Octopi() {
}
Octopi::~Octopi() {
}

void Octopi::Initialize(sf::RenderWindow* window) {
 m_renderWindow = window;
 m_octTexture = new sf::Texture();
 m_octTexture->loadFromFile("Assets/Images/OCTUPUS.png");
 m_octTextureAtc = new sf::Texture();
 m_octTextureAtc->loadFromFile("Assets/Images/OCTUPUSAtc.png");
 m_octSprite = new sf::Sprite();
 m_octSprite->setTexture(*m_octTexture);
 m_hitTexture = new sf::Texture();
 m_hitTexture->loadFromFile("Assets/Images/HIT.png");
 m_hitSprite = new sf::Sprite();
 m_hitSprite->setTexture(*m_hitTexture);
 m_clock = new sf::Clock();
 m_attackClock = new sf::Clock();
 m_hitSprite->scale(3.f, 3.f);

 m_scale = 0.1f;
 m_octSprite->setScale(m_scale, m_scale);

 m_hitTime = 0.f;
 //m_posY = 300.f;
 m_posY = 600.f;
 m_alpha = 10;
 m_attackTimer = 0.f;
 m_hit = false;

// std::cout << m_posX << std::endl;

 m_canDie = false;
 m_isDead = false;

 m_octSprite->setOrigin(m_octTexture->getSize().x/2, m_octTexture->getSize().y);
 m_octSprite->setPosition(m_posX,m_posY);
 m_octSprite->setScale(m_scale, m_scale);

}

void Octopi::Update(Player*	player) {
	  m_deltaTime = m_clock->restart().asSeconds();
	  m_attackTimer += m_deltaTime;

	  /*
	  if(m_octSprite->getScale().x < 1.0f){
		  if(m_octSprite->getColor().Transparent <;
		  m_octSprite->setColor(sf::Color::Magenta);
		m_octSprite->setScale(m_scale, m_scale);
		m_scale += 0.01f;
	  }
	  */

	  if(m_attackTimer < 2.0f){
		  m_scale += 0.5f * m_deltaTime;
		  m_alpha += 80 * m_deltaTime;
		  m_octSprite->setColor(sf::Color(m_octSprite->getColor().r, m_octSprite->getColor().g, m_octSprite->getColor().b, m_alpha));
		  if(m_octSprite->getScale().x < 2.0f){
			m_octSprite->setScale(m_scale, m_scale);
		  }
	  }else if(m_attackTimer < 2.5f){
		  m_canDie = true;
		  m_scale += 1.7f * m_deltaTime;
		  m_octSprite->setTexture(*m_octTextureAtc);
		  m_octSprite->setColor(sf::Color(m_octSprite->getColor().r, m_octSprite->getColor().g, m_octSprite->getColor().b, 255));
		  if(m_octSprite->getScale().x < 2.0f){
			m_octSprite->setScale(m_scale, m_scale);
		  }

	  }else if(m_attackTimer > 3.0f){
		  m_posY += 250 * m_deltaTime;
		  m_scale += 1.2f * m_deltaTime;
		  if(m_octSprite->getScale().x < 2.5f){
			m_octSprite->setScale(m_scale, m_scale);
		  }
		  m_octSprite->setPosition(m_posX,m_posY);
		  if(m_octSprite->getPosition().y - m_octSprite->getGlobalBounds().height > 720){
			m_isDead = true;
			player->takeDamage(10);
		  }
	  }
	  


	  if(m_hit == true){
		m_attackTimer == 0.f;
		m_scale = m_scale - 10.0f * m_deltaTime;
		m_posY -= 3000 * m_deltaTime;
		m_posX -= 1000 * m_deltaTime;
		m_octSprite->rotate(5);
		if(m_octSprite->getScale().x > 0.1f){
			m_octSprite->setScale(m_scale, m_scale);
		}
		m_octSprite->setPosition(m_posX,m_posY); 
		if(m_posY < 0){
			m_isDead = true;
			player->addScore(500);
		}
	  }

	  m_renderWindow->draw(*m_octSprite);
	
	  if(m_octSprite->getScale().x > 2.4f && m_hitTime < 0.2f){
		  m_hitSprite->setPosition(m_octSprite->getPosition().x - 50.f, m_octSprite->getPosition().y - 200.f);
		  m_renderWindow->draw(*m_hitSprite);
		  m_hitTime += m_deltaTime;
	 }

}

void Octopi::Cleanup() {
 delete m_octSprite;
 delete m_octTexture;
 delete m_clock;
 delete m_hitSprite;
 delete m_hitTexture;
}